using System;
using System.DirectoryServices;
using System.Collections;

namespace OLAActiveDirectory.Management
{
    public interface IADUser
    {
        DirectoryEntry DirectoryEntry { get;}
        bool IsUser { get;}
        PropertyValueCollection this[string strKey] { get;}
    }
    public class ADUser : IADUser
    {
        private readonly DirectoryEntry m_objUserEntry;
        public ADUser(string strLogin, string strRootPath)
        {
            DirectoryEntry objRootEntry = new DirectoryEntry(strRootPath);
            DirectorySearcher objADSearcher = new DirectorySearcher(objRootEntry);
            objADSearcher.Filter = "(&(objectClass=user)(anr=" + strLogin + "))";
            SearchResult objResult = objADSearcher.FindOne();
            this.m_objUserEntry = (objResult != null) ? objResult.GetDirectoryEntry() : null;
        }
        public DirectoryEntry DirectoryEntry
        {
            get
            {
                return this.m_objUserEntry;
            }
        }
        public PropertyValueCollection this[string strKey]
        {
            get
            {
                return this.m_objUserEntry.Properties[strKey];
            }
        }
        public bool IsUser
        {
            get
            {
                return this.m_objUserEntry != null;
            }
        }
    }
}

